﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace HJ212_Common
{
    public class TypeParser
    {
        /// <summary>
        /// 判断对象是否为Int32类型的数字
        /// </summary>
        /// <param name="Expression"></param>
        /// <returns></returns>
        public static bool IsNumeric(object Expression)
        {
            if (Expression != null)
            {
                string str = Expression.ToString();
                if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
                {
                    if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
                    {
                        return true;
                    }
                }
            }
            return false;

        }

        /// <summary>
        /// 是否是double类型
        /// </summary>
        /// <param name="Expression"></param>
        /// <returns></returns>
        public static bool IsDouble(object Expression)
        {
            if (Expression != null)
            {
                return Regex.IsMatch(Expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");

            }
            return false;
        }
        /// <summary>
        /// string型转换为double型
        /// </summary>
        /// <param name="Expression"></param>
        /// <returns></returns>
        public static double StrToDouble(string Expression)
        {
            double value = 0;
            if (!String.IsNullOrEmpty(Expression))
            {
                if (double.TryParse(Expression, out value))
                    return value;
                else
                    return 0;

            }
            return 0;
        }
        /// <summary>
        /// string型转换为double型,保留小数(四舍六进）
        /// </summary>
        /// <param name="Expression"></param>
        /// <returns></returns>
        public static double StrToDouble(string Expression, int decimals)
        {
            double value = 0;
            if (!String.IsNullOrEmpty(Expression))
            {
                if (double.TryParse(Expression, out value))
                    return Math.Round(value, decimals);
                else
                    return 0;

            }
            return 0;
        }
        /// <summary>
        /// string型转换为double型,保留小数(四舍六进）
        /// </summary>
        /// <param name="Expression"></param>
        /// <returns></returns>
        public static decimal StrToDecimal(string Expression, int decimals)
        {
            decimal value = 0;
            if (!String.IsNullOrEmpty(Expression))
            {
                if (decimal.TryParse(Expression, out value))
                    return Math.Round(value, decimals);
                else
                    return 0;

            }
            return 0;
        }
        /// <summary>
        /// 将字符转换为Int32类型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int StrToInt(string Expression)
        {

            Int32 value = 0;
            if (!String.IsNullOrEmpty(Expression))
            {
                if (Int32.TryParse(Expression, out value))
                    return value;
                else
                    return 0;

            }
            return 0;
        }
        /// <summary>
        /// 将字符转换为Int64类型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int64类型结果</returns>
        public static Int64 StrToInt64(string Expression)
        {

            Int64 value = 0;
            if (!String.IsNullOrEmpty(Expression))
            {
                if (Int64.TryParse(Expression, out value))
                    return value;
                else
                    return 0;

            }
            return 0;
        }
         /// <summary>
        /// 将字符转换为Int32类型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int StrToIntNull(string Expression)
        {

            Int32 value = 0;
            if (!String.IsNullOrEmpty(Expression))
            {
                if (Int32.TryParse(Expression, out value))
                    return value;
                else
                    return -1;

            }
            return -1;
        }
        /// <summary>
        /// 将对象换为Int32类型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int ObjectToInt(object Expression)
        {
            if (Expression == null)
                return 0;
            Int32 value = 0;
            if (!String.IsNullOrEmpty(Expression.ToString()))
            {
                if (Int32.TryParse(Expression.ToString(), out value))
                    return value;
                else
                    return 0;

            }
            return 0;
        }
        /// <summary>
        /// 将对象换为Double类型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static double ObjectToDouble(object Expression)
        {
            if (Expression == null)
                return 0;
            double value;
            if (!String.IsNullOrEmpty(Expression.ToString()))
            {
                if (double.TryParse(Expression.ToString(), out value))
                    return value;
                else
                    return 0;

            }
            return 0;
        }
        /// <summary>
        /// string型转换为bool型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的bool类型结果</returns>
        public static bool StrToBool(object Expression, bool defValue)
        {
            if (Expression != null)
            {
                if (string.Compare(Expression.ToString(), "true", true) == 0)
                {
                    return true;
                }
                else if (string.Compare(Expression.ToString(), "false", true) == 0)
                {
                    return false;
                }
            }
            return defValue;

        }

        /// <summary>
        /// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型
        /// </summary>
        /// <param name="strNumber">要确认的字符串数组</param>
        /// <returns>是则返加true 不是则返回 false</returns>
        public static bool IsNumericArray(string[] strNumber)
        {
            if (strNumber == null)
            {
                return false;
            }
            if (strNumber.Length < 1)
            {
                return false;
            }
            foreach (string id in strNumber)
            {
                if (!IsNumeric(id))
                {
                    return false;
                }
            }
            return true;

        }
        /// <summary>
        /// 将long型数值转换为Int32类型
        /// </summary>
        /// <param name="objNum"></param>
        /// <returns></returns>
        public static int SafeInt32(object objNum)
        {
            if (objNum == null)
            {
                return 0;
            }
            string strNum = objNum.ToString();
            if (IsNumeric(strNum))
            {

                if (strNum.ToString().Length > 9)
                {
                    if (strNum.StartsWith("-"))
                    {
                        return int.MinValue;
                    }
                    else
                    {
                        return int.MaxValue;
                    }
                }
                return Int32.Parse(strNum);
            }
            else
            {
                return 0;
            }
        }
    }
}
